/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.InvalidArgumentException;
import javax.sip.header.MinExpiresHeader;

/**
 * The Min-Expires header field conveys the minimum refresh interval supported
 * for soft-state elements managed by that server. This includes Contact
 * header fields that are stored by a registrar. The header field contains a
 * decimal integer number of seconds from 0 to (2**32)-1.
 * <p>
 * Allowing the registrar to set the registration interval protects it against
 * excessively frequent registration refreshes while limiting the state that it
 * needs to maintain and decreasing the likelihood of registrations going
 * stale. The expiration interval of a registration is frequently used in the
 * creation of services.  An example is a follow-me service, where the user may
 * only be available at a terminal for a brief period. Therefore, registrars
 * should accept brief registrations; a request should only be rejected if the
 * interval is so short that the refreshes would degrade registrar performance.
 * <p>
 * If a User Agent receives a 423 (Interval Too Brief) response to a REGISTER request,
 * it MAY retry the registration after making the expiration interval of all
 * contact addresses in the REGISTER request equal to or greater than the
 * expiration interval within the Min-Expires header field of the 423
 * (Interval Too Brief) response. The Min-Expires header field that states the
 * minimum expiration interval the registrar is willing to honor.
 * <p>
 * For Example:<br>
 * <code>Min-Expires: 60</code>
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class MinExpiresHeaderImpl extends HeaderBase implements MinExpiresHeader {
    private int expires;

    private static final MinExpiresHeaderParser parser = new MinExpiresHeaderParser();

    public MinExpiresHeaderImpl() {
        super(MinExpiresHeader.NAME);
    }

    MinExpiresHeaderImpl(byte[] data, int start, int end) {
        super(MinExpiresHeader.NAME, data, start, end);
    }

    public Object clone() {
        final MinExpiresHeaderImpl header = new MinExpiresHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.expires = this.expires;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return Integer.toString(expires);
    }

    /**
     * Sets the relative expires value of the ExpiresHeader in units of seconds.
     * The expires value MUST be between zero and (2**31)-1.
     *
     * @param expires the new expires value of this ExpiresHeader
     * @throws javax.sip.InvalidArgumentException
     *          if supplied value is less than zero.
     */

    public void setExpires(int expires) throws InvalidArgumentException {
        parse();
        this.expires = expires;
        invalidateHeaderData();
    }

    /**
     * Gets the expires value of the ExpiresHeader. This expires value is
     * relative time.
     *
     * @return the expires value of the ExpiresHeader.
     */

    public int getExpires() {
        parse();
        return this.expires;
    }
}
/*
 * $Log$
 */